#pragma once

#include <Windows.h>
#include <WindowsX.h>
#include <tchar.h>

#include <GdiPlus.h>
using namespace Gdiplus;

#include <string>
using namespace std;


#ifndef UNICODE  
typedef std::string String;
#else
typedef std::wstring String; 
#endif


class Win32Application
{
public:
	Win32Application();
	virtual ~Win32Application();

	// application functions
	LRESULT Create(int width, int height, const TCHAR* title);
	void Release();
	void Close();
	int Run();
	void Refresh();

	// windows message handlers
	virtual HRESULT OnCreate();
	virtual void OnRelease();
	virtual bool OnMessage(UINT, WPARAM, LPARAM);
	virtual void OnKeyDown(DWORD key);
	virtual void OnChar(char c);
	virtual void OnPaint(Graphics& graphics);
	virtual void OnMouseDown(DWORD button, int x, int y);
	virtual void OnMouseUp(DWORD button, int x, int y);
	virtual void OnMouseMove(DWORD button, int x, int y);

	// utility functions
	String GetAppFileName() const;
	String GetAppDir() const;
	String ChangeFileExt(String fileName, String extension);
	bool FileExists(String fileName);
	bool OpenFileDialog(const TCHAR *filter, String initialDir, String& fileName) const;

	// accessors
	inline HWND Handle() const { return m_hwnd; }

protected:
	BOOL CreateWin32Application(int width, int height, const TCHAR* title);

private:
	static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

	void DisposeBackbuffer();
	HDC GetBackbuffer();

	LONG		m_width;
	LONG		m_height;

	HWND		m_hwnd;

	ULONG_PTR	m_gdip;
	HDC			m_memoryDC;
	HBITMAP		m_memoryBmp;
};

